package org.gdstash.db;

import org.gdstash.file.ARZRecord;
import org.gdstash.util.GDMsgFormatter;
import org.gdstash.util.GDMsgLogger;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DBEngineMasteryTier {
    private static final String TABLE_NAME = "GDC_MASTERY_TIER";
    private static final int ROW_ID = 1;
    private static final int ROW_INDEX = 2;
    private static final int ROW_MASTERY_LEVEL = 3;
    private String id;
    private int index;
    private int masteryLevel;

    public DBEngineMasteryTier() {
    }

    public DBEngineMasteryTier(final int index, final int masteryLevel) {
        this.id = "DEFAULT";
        this.index = index;
        this.masteryLevel = masteryLevel;
    }

    public int getMasteryTier() {
        return this.index + 1;
    }

    public int getMasteryLevel() {
        return this.masteryLevel;
    }

    public static void createTable(final Connection conn) throws SQLException {
        final String dropTable = "DROP TABLE GDC_MASTERY_TIER";
        final String createTable = "CREATE TABLE GDC_MASTERY_TIER (ID            VARCHAR(8) NOT NULL, INDEX         INTEGER, MASTERY_LEVEL INTEGER, PRIMARY KEY (ID, INDEX))";
        try (final Statement st = conn.createStatement()) {
            if (GDDBUtil.tableExists(conn, "GDC_MASTERY_TIER")) {
                st.execute(dropTable);
            }
            st.execute(createTable);
            st.close();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            final Object[] args = {"GDC_MASTERY_TIER"};
            final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_CREATE_TABLE", args);
            GDMsgLogger.addError(msg);
            throw ex;
        }
    }

    public static void delete(final Connection conn) throws SQLException {
        final String deleteEntry = "DELETE FROM GDC_MASTERY_TIER";
        try (final PreparedStatement ps = conn.prepareStatement(deleteEntry)) {
            ps.executeUpdate();
            ps.close();
        } catch (SQLException ex) {
            conn.rollback();
        }
    }

    public static void insert(final Connection conn, final ARZRecord record, final DBEngineMasteryTier masteryTier) throws SQLException {
        final String insert = "INSERT INTO GDC_MASTERY_TIER VALUES (?,?,?)";
        try (final PreparedStatement ps = conn.prepareStatement(insert)) {
            ps.setString(1, "DEFAULT");
            ps.setInt(2, masteryTier.index);
            ps.setInt(3, masteryTier.masteryLevel);
            ps.executeUpdate();
            ps.clearParameters();
            ps.close();
            conn.commit();
        }
    }

    public static List<DBEngineMasteryTier> get() {
        List<DBEngineMasteryTier> list = null;
        final String command = "SELECT * FROM GDC_MASTERY_TIER ORDER BY INDEX";
        try (final Connection conn = GDDBData.getConnection();
             final PreparedStatement ps = conn.prepareStatement(command)) {
            try (final ResultSet rs = ps.executeQuery()) {
                list = wrap(rs);
                conn.commit();
            } catch (SQLException ex) {
                throw ex;
            }
        } catch (SQLException ex2) {
            final Object[] args = {"-", "GDC_MASTERY_TIER"};
            final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_READ_TABLE_BY_ID", args);
            GDMsgLogger.addError(msg);
            GDMsgLogger.addError(ex2);
        }
        return list;
    }

    private static List<DBEngineMasteryTier> wrap(final ResultSet rs) throws SQLException {
        final List<DBEngineMasteryTier> list = new ArrayList<DBEngineMasteryTier>(8);
        while (rs.next()) {
            final DBEngineMasteryTier mt = new DBEngineMasteryTier();
            mt.id = rs.getString(1);
            mt.index = rs.getInt(2);
            mt.masteryLevel = rs.getInt(3);
            list.add(mt);
        }
        return list;
    }
}
